
#include "member_BScrollArea.h"
#include "member_BScrollBar.h"

using namespace BWE;

member_BScrollArea::member_BScrollArea(BScrollArea* scrollArea)
{
	boss = scrollArea;
	hScrollBar = 0;
	hScrollBarPolicy = ScrollBarPolicy_Auto;
	vScrollBar = 0;
	vScrollBarPolicy = ScrollBarPolicy_Auto;
	vstep = 10;
	hstep = 10;
}
member_BScrollArea::~member_BScrollArea()
{

}

void member_BScrollArea::procVerticalScrollStepChanged(BObject* object, const BValue& value)
{

}
void member_BScrollArea::procVerticalScrolling(BObject* object, const BValue& value)
{
	BRect crect = boss->clientRect();
	int xoffset = hScrollBar->value();
	int yoffset = vScrollBar->value();
	int x = crect.x() - xoffset;
	int y = crect.y() - yoffset;
	area.setPos(x, y);
	boss->scrollArea();
	boss->fresh();
}

void member_BScrollArea::procHorizontalScrollStepChanged(BObject* object, const BValue& value)
{
	hstep = value;
}
void member_BScrollArea::procHorizontalScrolling(BObject* object, const BValue& value)
{
	BRect crect = boss->clientRect();
	int xoffset = hScrollBar->value();
	int yoffset = vScrollBar->value();
	int x = crect.x() - xoffset;
	int y = crect.y() - yoffset;
	area.setPos(x, y);
	boss->scrollArea();
	boss->fresh();
}

void member_BScrollArea::adjustScrollBar()
{
	int border = boss->border();
	int width = boss->width();
	int height = boss->height();

	const BRect& crect = boss->clientRect();
	const BPerchs& perchs = boss->perchs();
	const BMargins& margins = boss->margins();

	if (hScrollBar && hScrollBarPolicy)
	{
		if (hScrollBarPolicy == ScrollBarPolicy_Never)
		{
			hScrollBar->setAble(false);
			hScrollBar->setVisible(false);
		}
		else if (hScrollBarPolicy == ScrollBarPolicy_Auto)
		{
			hScrollBar->setVisible(crect.width() < area.width());
		}
		else if (vScrollBarPolicy == ScrollBarPolicy_Always)
		{
			hScrollBar->setVisible(true);
		}
		BRect bottomPerch = boss->rect(Part_Bottom);
		hScrollBar->setRect(bottomPerch);
		hScrollBar->setRange(0, area.width());
		hScrollBar->setPage(crect.width());
		hScrollBar->setValue(crect.left() - area.left());
		hScrollBar->raise();
	}

	if (vScrollBar && vScrollBarPolicy)
	{
		if (vScrollBarPolicy == ScrollBarPolicy_Never)
		{
			vScrollBar->setAble(false);
			vScrollBar->setVisible(false);
		}
		else if (vScrollBarPolicy == ScrollBarPolicy_Auto)
		{
			vScrollBar->setVisible(crect.height() < area.height());
		}
		else if (vScrollBarPolicy == ScrollBarPolicy_Always)
		{
			vScrollBar->setVisible(true);
		}
		BRect rigth = boss->rect(Part_Right);
		vScrollBar->setRect(rigth);
		vScrollBar->setRange(0, area.height());
		vScrollBar->setPage(crect.height());
		vScrollBar->setValue(crect.top() - area.top());
		vScrollBar->raise();
	}
}
